package admin

import (
	"net/url"
	"strconv"

	controllers "github.com/cn-joyconn/goadmin/controllers"
	adminModel "github.com/cn-joyconn/goadmin/models/admin"
	"github.com/cn-joyconn/goadmin/models/global"

	adminServices "github.com/cn-joyconn/goadmin/services/admin"
	"github.com/gin-gonic/gin"
)

type AppController struct {
	controllers.BaseController
}

var appService *adminServices.AdminAppService

func init() {
	appService = &adminServices.AdminAppService{}
}

func (controller *AppController) ManagePage(c *gin.Context) {
	// fmt.Printf(c.HandlerName())
	data := gin.H{
		"pageTitle": "应用管理",
	}

	controller.ResponseHtml(c, "admin/app.html", data)
}

/**
 * 修改应用
 *
 * @param
 * @return
 */
func (controller *AppController) UpdateState(c *gin.Context) {
	//Integer Id, Integer state, HttpServletRequest request
	fId := c.PostForm("Id")
	Id, err1 := strconv.Atoi(fId)
	if err1 != nil {
		controller.ApiErrorCode(c, "参数错误", "", global.ParamsError)
		return
	}
	fstate := c.PostForm("state")
	state, err2 := strconv.Atoi(fstate)
	if err2 != nil {
		controller.ApiErrorCode(c, "参数错误", "", global.ParamsError)
		return
	}
	//  uid := controller.GetContextUserId(c)

	result := appService.UpdateStateByPrimaryKey(Id, state)
	if result > 0 {
		controller.ApiSuccess(c, "修改成功", result)
	} else {
		controller.ApiError(c, "修改失败", result)
	}
}

/**
 * 删除应用
 *
 * @param
 * @return
 */
func (controller *AppController) DeleteApp(c *gin.Context) {
	//Integer menuID,  HttpServletRequest request
	fId := c.PostForm("appID")
	appID, err1 := strconv.Atoi(fId)
	if err1 != nil {
		controller.ApiErrorCode(c, "参数错误", "", global.ParamsError)
		return
	}

	result := appService.DeleteByID(appID)
	if result > 0 {
		controller.ApiSuccess(c, "删除成功", result)
	} else {
		controller.ApiError(c, "删除失败", result)
	}

}

/**
 * 添加应用
 *
 * @param record
 * @return
 */
func (controller *AppController) InsertApp(c *gin.Context) {
	var record adminModel.AdminApp
	c.ShouldBindJSON(&record)
	PDesc, err1 := url.QueryUnescape(record.PDesc)
	if err1 == nil {
		record.PDesc = PDesc
	}
	PName, err2 := url.QueryUnescape(record.PName)
	if err2 == nil {
		record.PName = PName
	}
	result := appService.Insert(&record)
	if result > 0 {
		// result = record.PId
		err, resultObj := appService.SelectAppById(record.PId)
		if err != nil {
			controller.ApiErrorCode(c, "添加失败", nil, global.ServiceError)
		} else {
			controller.ApiSuccess(c, "添加成功", resultObj)
		}
	} else {
		controller.ApiError(c, "添加失败", nil)
	}

}

/**
 * 修改菜单
 *
 * @param record
 * @return
 */
func (controller *AppController) UpdatetApp(c *gin.Context) {
	// JoyConnAuthenticatePermissionMenuModel record, HttpServletRequest request
	var record adminModel.AdminApp
	err := c.ShouldBindJSON(&record)
	if err != nil {
		controller.ApiErrorCode(c, "参数错误，"+err.Error(), nil, global.ParamsError)
		return
	}
	PDesc, err1 := url.QueryUnescape(record.PDesc)
	if err1 == nil {
		record.PDesc = PDesc
	}
	PName, err2 := url.QueryUnescape(record.PName)
	if err2 == nil {
		record.PName = PName
	}
	result := appService.UpdateByPrimaryKey(&record)
	if result > 0 {
		err, resultObj := appService.SelectAppById(record.PId)
		if err != nil {
			controller.ApiErrorCode(c, "修改失败", nil, global.ServiceError)
		} else {
			controller.ApiSuccess(c, "修改成功", resultObj)
		}
	} else if result == 0 {
		controller.ApiErrorCode(c, "未修改任何内容", result, global.NoAffected)
	} else {
		controller.ApiError(c, "修改失败", nil)
	}

}

/**
 * 查询应用列表
 *
 * @param menuID
 * @return
 */
func (controller *AppController) GetByPage(c *gin.Context) {
	//Integer menuID, HttpServletRequest request
	fId := c.Query("pageIndex")
	pageIndex, err1 := strconv.Atoi(fId)
	if err1 != nil {
		controller.ApiErrorCode(c, "参数错误", "", global.ParamsError)
		return
	}
	fId = c.Query("pageSize")
	pageSize, err2 := strconv.Atoi(fId)
	if err2 != nil {
		controller.ApiErrorCode(c, "参数错误", "", global.ParamsError)
		return
	}
	stateStr := c.Query("state")
	state, err3 := strconv.Atoi(stateStr)
	if err3 != nil {
		controller.ApiErrorCode(c, "参数错误", "", global.ParamsError)
		return
	}
	userID := controller.GetContextUserId(c)
	if global.IsSuperAdmin(uint64(userID)) {
		userID = 0
	}
	err, result, count := appService.SelectByPage(state, pageIndex, pageSize)
	if err == nil {
		controller.ApiDataList(c, "", result, count)
	} else {
		controller.ApiErrorCode(c, "", result, global.NoResult)
	}

}

/**
 * 查询应用
 *
 * @param menuID
 * @return
 */
func (controller *AppController) GetOne(c *gin.Context) {
	//Integer menuID, HttpServletRequest request
	fId := c.Query("id")
	id, err1 := strconv.Atoi(fId)
	if err1 != nil {
		controller.ApiErrorCode(c, "参数错误", "", global.ParamsError)
		return
	}
	err, result := appService.SelectAppById(id)
	if err == nil {
		controller.ApiSuccess(c, "", result)
	} else {
		controller.ApiErrorCode(c, "", result, global.NoResult)
	}

}

/**
 * 查询应用
 *
 * @param menuID
 * @return
 */
func (controller *AppController) GetApps(c *gin.Context) {
	//Integer menuID, HttpServletRequest request
	var appids []int
	err := c.ShouldBind(&appids)
	if err != nil {
		controller.ApiErrorCode(c, "参数错误", "id参数错误", global.ParamsError)
		return
	}
	result := appService.SelectByAppIDs(appids)
	controller.ApiSuccess(c, "", result)

}
